/////////////////////////////////////////////////////////////
// File Name	:	"CBitmapFont.cpp"
//
// Author Name	:	Brandon Layton (BL)
//
// Purpose:		:	Used to render image text to the screen
/////////////////////////////////////////////////////////////
#include "BitmapFont.h"
#include "../Systems/TextureManager.h"
#include "../Systems/Direct3D.h"
#include <fstream>

using namespace std;

CBitmapFont::CBitmapFont(char * szFileName)
{
	m_nFontTextureID = -1;
	
	char imagepath[MAX_PATH] = "resource/fonts/";
	ifstream stream(szFileName, ios_base::binary);

	if(stream.is_open())
	{
		stream.beg;
		CharRect ch;
		RECT rect;
		int num;
		stream.read((char *)&num, sizeof(int));
		char path[255];
		stream.read(path, sizeof(char) * num);
		path[num] = '\0';
		strcat_s((char *)&imagepath, MAX_PATH, path);
		m_nFontTextureID = CTextureManager::GetInstance()->LoadTexture(imagepath, D3DCOLOR_ARGB(255, 0, 0, 0));
		stream.read((char *)&num, sizeof(int));
		for(int i = 0; i < num; i++)
		{
			int id, x, y, width, height, xoffset, yoffset, xadvance;

			stream.read((char *)&id, sizeof(int));
			stream.read((char *)&x, sizeof(int));
			stream.read((char *)&y, sizeof(int));
			stream.read((char *)&width, sizeof(int));
			stream.read((char *)&height, sizeof(int));
			stream.read((char *)&xoffset, sizeof(int));
			stream.read((char *)&yoffset, sizeof(int));
			stream.read((char *)&xadvance, sizeof(int));

			rect.left = x;
			rect.top = y;
			//rect.left += xoffset;
			//rect.top += yoffset;
			rect.bottom = rect.top + height;
			rect.right = rect.left + width;
			ch.imageRect = rect;
			ch.xOffset = xoffset;
			ch.yOffset = yoffset;
			m_rects[(char)id] = ch;
		}
		
		
		
		

		stream.close();
	}


}

CBitmapFont::~CBitmapFont()
{
	CTextureManager::GetInstance()->UnloadTexture(m_nFontTextureID);
}

void CBitmapFont::DrawText(char * szText, float x, float y, int maxWidth, float fScale, DWORD color)
{
	int charCount = strlen(szText);

	float xOffset, yOffset;

	xOffset = yOffset = 0;


	for(int i = 0; i < charCount; i++)
	{
		
		CharRect ch = m_rects[szText[i]];
		RECT drawRect = ch.imageRect;
		if(szText[i] == '\n')
		{
			xOffset = 0;
			yOffset += 42 * fScale;
		}
		else if(szText[i] == ' ')
		{
			xOffset += 30 * fScale;
			continue;
		}

		int nextWordWidth = 0;

		for(int j = i; j < charCount && szText[j] != '\0' && szText[j] != '\n' && szText[j] != ' '; j++)
		{
			nextWordWidth += m_rects[szText[j]].imageRect.right - m_rects[szText[j]].imageRect.left;
		}

		if(xOffset + (nextWordWidth * fScale) >= maxWidth)
		{
			xOffset = 0;
			yOffset += 42 * fScale;
		}

		CTextureManager::GetInstance()->Draw(m_nFontTextureID, x + xOffset, y + yOffset + (ch.yOffset * fScale), 0.0f, 0.0f, fScale, fScale, &drawRect,NULL, 0.0f, 0.0f, 0.0f, color);
		
		xOffset += (drawRect.right - drawRect.left) * fScale;

		if(xOffset > maxWidth)
		{
			xOffset = 0;
			yOffset += 42 * fScale;
		}
	}

}
